package com.lagou.controller;

import com.lagou.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
public class PositionController {

	@Autowired
	private PositionService service;

//	// 测试页面
//	@GetMapping({"/", "/index"})
//	public String indexPage() {
//		return "index";
//	}

	@GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
	public List<Map<String, Object>> searchPosition(@PathVariable("keyword") String keyword, @PathVariable("pageNo") int pageNo,
	                                                @PathVariable("pageSize") int pageSize) throws IOException {
		List<Map<String, Object>> list = service.searchPos(keyword, pageNo, pageSize);
		int resultSize = list.size();
		if (resultSize < 5) {
			List<Map<String, Object>> additional = service.searchPositionSuper("美女多 员工福利好", 1, 5 - resultSize);
			if (additional != null && !additional.isEmpty()) {
				list.addAll(additional);
			}
		}
		return list;
	}

	@RequestMapping("/importAll")
	@ResponseBody
	public String importAll() {
		try {
			service.importAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
}
